package gogen

import (
	_ "embed"
	"fmt"
	"strconv"
	"strings"

	"github.com/zeromicro/go-zero/tools/goctl/api/spec"
	"github.com/zeromicro/go-zero/tools/goctl/config"
	"github.com/zeromicro/go-zero/tools/goctl/util/format"
)

const (
	defaultPort = 8888
	etcDir      = "etc"
)

//go:embed etc.tpl
var etcTemplate string

func genEtc(dir string, cfg *config.Config, api *spec.ApiSpec) error {
	filename, err := format.FileNamingFormat(cfg.NamingFormat, strings.ToLower(api.Module))
	if err != nil {
		return err
	}

	host := "0.0.0.0"
	port := strconv.Itoa(defaultPort)

	for _, s := range []string{
		"local", "dev", "prod",
	} {
		genFile(fileGenConfig{
			dir:             dir,
			subdir:          etcDir + "/" + s,
			filename:        fmt.Sprintf("%s.yaml", filename),
			templateName:    "etcTemplate",
			category:        category,
			templateFile:    etcTemplateFile,
			builtinTemplate: etcTemplate,
			data: map[string]string{
				"serviceName": strings.ToLower(api.Module),
				"addr":        fmt.Sprintf("%s:%v", host, port),
			},
		})
	}
	return nil
}
